package cn.com.dycc.util;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;






/**
 * 日志工具类
 * @author gaofan
 *
 */
public class LogUtil {
	
	
	/**
	 * 新增系统操作日志
	 * @param request 请求对象
	 * @param operate 操作内容
	 * @param result 操作结果
	 * @return 插入系统操作日志 true 成功  false 失败
	 */
	
    public static boolean addT_Operate_Log(HttpServletRequest request,String operate,String result) {
		
		HttpSession session=request.getSession();
		String user_id=(String)session.getAttribute("id");
		String account=(String)session.getAttribute("account");
		String username=(String)session.getAttribute("username");
		String org_id=(String)session.getAttribute("org_id");
		Date login_date=Date.valueOf(DateUtil.getNowDate(DateUtil.DATE_FMT));
		String ip=IPUtil.getLoginIp(request);
		Timestamp create_date=Timestamp.valueOf(DateUtil.getNowDate(DateUtil.TIME_FMT));
		Connection conn=null;
		try {
			conn=DBUtil.getConnection();
			String sql=""+
				"insert into t_operate_log "+
				"(                         "+
				"  user_id,                "+	  
				"  account,                "+
				"  username,               "+
				"  login_date,             "+
				"  ip,                     "+
				"  operate,                "+	  
				"  org_id,                 "+
				"  create_date,            "+
				"  result                  "+
				")                         "+
				"values(                   "+
				"  ?,                      "+  
				"  ?,                      "+
				"  ?,                      "+
				"  ?,                      "+
				"  ?,                      "+
				"  ?,                      "+	  
				"  ?,                      "+
				"  ?,                      "+
				"  ?                       "+
				") " ;
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, user_id);
			ps.setString(2, account);
			ps.setString(3, username);
			ps.setDate(4, login_date);
			ps.setString(5, ip);
			ps.setString(6, operate);
			ps.setString(7, org_id);
			ps.setTimestamp(8, create_date);
			ps.setString(9, result);
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
			
		}finally{
			if(conn!=null){
				DBUtil.close();
			}
		}
		
		
	}
	

}
